<template>
    <div>
        <van-field label='标题' placeholder="请输入标题" v-model="form.title"/>
        <van-field label='介绍' placeholder="请输入介绍" v-model="form.descrip"/>
        <van-field label='图片' placeholder="图片" v-model="form.pic"/>
        <van-field name="radio" label="收费类型">
    <template #input>
            <van-radio-group v-model="form.status" direction="horizontal">
            <van-radio name="1">免费</van-radio>
            <van-radio name="2">收费</van-radio>
            </van-radio-group>
    </template>
        </van-field>
         选择老师<select name="" id="" v-model="form.teacher_id">
            <option :value="i.id" v-for="i in t_list">{{i.name}}</option>
        </select>
        <br><br><br>
        选择分类
        <reply :data='cates'/>
        <br><br><br>
        动态参数
        <div v-if="params">
            <van-field v-for="(v,k) in params" :label="v" placeholder="请输入动态参数" v-model="values[v]"/>
        </div>
        <van-button @click="add">添加</van-button>

    </div>
</template>

<script>
//导入子组件
import bus from './bus.js'
import reply from './reply'
    export default {
        //注册子组件
        components:{
            'reply':reply
        },

        data(){
            return{
                
                t_list:[{'id':1,'name':'张老师'},{'id':2,'name':'王老师'}],
                cates:{},
                params:{},
                form:{pid:0,params:'','status':'2'},
                values:{}
            }
        },
        methods:{
             get_catelist(){
                this.$axios.get('cateM')
                .then(res=>{
                    this.cates={'id':0,'name':'333','son':res.data.list}
                })
            },
            add(){
                console.log(this.values)
                if(this.values){
                    this.form.params=JSON.stringify(this.values)
                }console.log(this.form)

                this.$axios.post('courseM',this.form)
                .then(res=>{
                    
                })
            },
        },
        mounted(){
            this.get_catelist()
            //监听
            bus.$on('pid',val=>{
                //更新pid
                this.form.pid=val
               //根据pid查询分类，查询分类中的params
               this.$axios.get('getCateByid?id='+val)
               .then(res=>{
                if(res.data.params){
                    this.params=JSON.parse( res.data.params)
                }else{
                    this.params={}
                }
                   
               })
            })
        },
    }
</script>

<style scoped>

</style>